import axios from "axios";


export function get(url: string, params: any | object = {}) {
    return new Promise((resolve, reject) => {
        axios.get<any>(url, {params: params})
            .then((response) => {
                resolve(response.data);
            })
            .catch((err) => {
                reject(err);
            });
    });
}

/*
 * 封装post方法
 * */
export function post(url: string, data: any = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then((response) => {
                resolve(response.data);
            })
            .catch((err) => {
                reject(err);
            });
    });
}

export function post2(url: string, params: any = {}) {
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: "POST",
            params: params,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
            }
        })
            .then((response) => {
                resolve(response.data);
            })
            .catch((err) => {
                reject(err);
            });
    });
}

export function post3(url: string, params: any = {}) {
    let formData = new FormData()
    // formData.append('params', params)
    for (let i in params) {
        if (params[i] === undefined) {
            formData.append(i, '')
        } else {
            formData.append(i, params[i])
        }
    }
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: "POST",
            data: formData,
            headers: {
                'Content-Type': 'multipart/form-data',
            }
        })
            .then((response) => {
                resolve(response.data);
            })
            .catch((err) => {
                reject(err);
            });
    });
}


export function postForm(url: string, params = {}) {
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: "POST",
            data: params,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
            }
        })
            .then((response) => {
                resolve(response.data);
            })
            .catch((err) => {
                reject(err);
            });
    });
}

export function postByParams(url: string, params = {}) {
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: "POST",
            params: params,
        })
            .then((response) => {
                resolve(response.data);
            })
            .catch((err) => {
                reject(err);
            });
    });
}

export function postSocket(url: string, params = {}) {
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: "POST",
            data: params,
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
            }
        })
            .then((response) => {
                resolve(response.data);
            })
            .catch((err) => {
                reject(err);
            });
    });
}
